// pages/operation/index.js
const app = getApp();
const domain = app.globalData.domain

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    filterList: [],
    activeTab: 'home'
  },
  changeTab(e) {
    console.log(e)
    this.setData({
      activeTab: e.currentTarget.dataset.tab
    })
  },
  toUrl(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },
  toLocaltion(e) {
    console.log(e)
    if (!e.currentTarget.dataset.img) {
      wx.showToast({
        title: '暂未配置位置',
        icon: 'none'
      })
    } else {
      wx.navigateTo({
        url: '../map/index?img=' + e.currentTarget.dataset.img,
      })
    }
  },
  exit() {
    wx.removeStorageSync('token')
    wx.redirectTo({
      url: '../login/login',
    })
  },
  onBack() {
    console.log(1)
    wx.navigateBack({
      delta: 1
    })
  },
  funNull() {},
  edit(e) {
    console.log(e.currentTarget.dataset)
    const {
      id,
      coord,
      mapurl,
      monurl,
      name,
      remark
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: `../settingsArea/index?id=${id}&coord=${coord||''}&name=${name||''}&remark=${remark||''}&mapUrl=${mapurl||''}&monUrl=${monurl||''}`,
    })
  },
  toMon(e) {
    console.log(e.currentTarget.dataset)
    const {
      id,
      coord,
      mapurl,
      monurl,
      name,
      remark
    } = e.currentTarget.dataset
    if (!e.currentTarget.dataset.monurl) {
      wx.showToast({
        title: '暂未配置视频',
        icon: 'none'
      })
      return
    }
    wx.navigateTo({
      url: `../mon/mon?monurl=${encodeURIComponent(monurl)}`,
    })
  },
  search(e) {
    const {
      value
    } = e.detail
    console.log(value)
    const filterList = this.data.list?.filter(item => {
      return item?.name?.indexOf(value) !== -1
    })
    this.setData({
      searchVal: value,
      filterList
    })
  },
  init() {
    wx.request({
      url: `${domain}/admin-api/smc/scene/list?pageSize=100`,
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: res => {
        if (res?.data?.error === "Unauthorized" || res?.data?.code == 401) {
          wx.removeStorageSync('token')
          wx.reLaunch({
            url: '../login/login',
          })
          return;
        }
        console.log(res)
        const list = res?.data?.data?.list
        let filterList = res?.data?.data?.list
        if (this.data.searchVal) {
          filterList = res?.data?.list?.filter(item => {
            return item?.name?.indexOf(this.data.searchVal) !== -1
          })
        }

        this.setData({
          list,
          filterList
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init()
    const inter = setInterval(() => {
      this.init()
    }, 5000)
    this.setData({
      inter
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    clearInterval(this.data.inter)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})